using System;
using System.Windows.Threading;

namespace ShimsDemo.SystemUnderTest
{
    public class CustomerViewModel : ViewModelBase
    {
        private Customer customer;
        private readonly ICustomerRepository repository;

        public CustomerViewModel()
        {
            customer = new Customer();
        }

        public CustomerViewModel(Customer customer, ICustomerRepository repository)
        {
            this.customer = customer;
            this.repository = repository;
        }

        public string CustomerName
        {
            get { return customer.Name; }
            set
            {
                customer.Name = value;
                RaisePropertyChanged("CustomerName");
            }
        }

        public void Save()
        {
            customer.LastUpdated = DateTime.Now;
            customer = repository.SaveOrUpdate(customer);
        }

        public void StartTimer()
        {
            var timer = new DispatcherTimer();
            var count = 30;
            timer.Tick += (s, e) =>
               {
                   count -= 1;
                   if (count == 0)
                   {
                       count = 30;
                       RefreshData();
                   }
               };

            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();
        }

        public void RefreshData()
        {
            // TODO: Load some data from the repository
        }

    }
}